package com.xzj.common.config;

import com.xzj.common.core.exception.ErrorPageException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Collections;
import java.util.Map;

/**
 * @Category com.xzj.com.xzj.basic.config
 * @Description 【自定义errorPage,直接继承 BasicErrorController】
 * @Author xiaj07
 * @Since 2022/11/17 16:30
 **/
@RestController
public class ErrorPageConfig extends BasicErrorController {

    public ErrorPageConfig() {
        super(new DefaultErrorAttributes(), new ErrorProperties());
    }

    @Override
    @RequestMapping(produces = {"text/html"})
    public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
        doError(request);
        return null;
    }

    @Override
    @RequestMapping
    public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
        doError(request);
        return null;
    }

    private void doError(HttpServletRequest request) {
        Map<String, Object> model = Collections.unmodifiableMap(
            this.getErrorAttributes(request, this.getErrorAttributeOptions(request, MediaType.TEXT_HTML)));
        //抛出ErrorPageException异常，方便被ExceptionHandlerConfig处理
        String path = model.get("path").toString();
        String status = model.get("status").toString();
        //静态资源文件发生404，无需抛出异常
        if (!path.contains("/common/") && !path.contains(".")) {
            throw new ErrorPageException(status, path);
        }
    }

}
